package gui.panels.jtree.filesystem;

import java.io.File;

import gui.panels.jtree.CategoryInfo;

import javax.swing.tree.*;

/** This class forces "male" nodes to have leaf icons and
forbids male childbaring ability */
public class FileSystemCategoryNode extends DefaultMutableTreeNode  {

	private	FileSystemCategoryInfo info = null;

	public FileSystemCategoryNode(CategoryInfo info) {
		
		
		super(info);
		if(info instanceof FileSystemCategoryInfo){
			this.info= (FileSystemCategoryInfo) info;
		}else{
			//TODO Mensaje de error
		}
		
	}

	/** Override a few methods... */
	public boolean isLeaf() {
		//Note: Male == true;
		
		File f = new File(info.getURL());
		return f.isFile();
	}

	public FileSystemCategoryInfo getInfo() {
		return info;
	}

	public void setInfo(FileSystemCategoryInfo info) {
		this.info = info;
	}

	public boolean getAllowsChildren() {
		File f = new File(info.getURL());
		
		return f.isDirectory();
	}

	public void add(DefaultMutableTreeNode child) {
		super.add(child);
		//System.out.println(child + " added to " + this);

		CategoryInfo childPI = (CategoryInfo) ((FileSystemCategoryNode) child).getUserObject();

		CategoryInfo oldParent = childPI.getParent();
		//if (parent != null) oldParent.remove(childPI);

		CategoryInfo newParent = (CategoryInfo) getUserObject();

		newParent.add(childPI);
	}

	public void remove(DefaultMutableTreeNode child) {
		super.remove(child);
		//System.out.println(child + " removed from " + this);

		CategoryInfo childPI = (CategoryInfo) ((FileSystemCategoryNode) child).getUserObject();

		CategoryInfo ParentPI = (CategoryInfo) getUserObject();
		if (parent != null) ParentPI.remove(childPI);
	}
	
	
}
